/**
 * js
 */
angular.module('mall.shopcart.base', [
        "ngStorage"
    ])

    .config(['$stateProvider', '$urlRouterProvider',
        function ($stateProvider, $urlRouterProvider) {
            $stateProvider
                .state('mall.shopcart', {
                    url: '/shopcart',
                    'abstract': true,
                    template: '<ui-view/>',
                    controller: 'ShopCartBaseController'
                })
            ;
        }])

    //购物车控制器
    .controller('ShopCartBaseController', ['$scope', '$state', '$localStorage', 'ShopcartService', 'MallAuthService',
        function ($scope, $state, $localStorage, ShopcartService, MallAuthService) {
            $scope.userId = MallAuthService.getUserid();
            if($scope.userId!=''){
                MallAuthService.getShopCartMessage().$promise.then(function(data){
                    var shops = {};
                    for(var i=0;i<data.length;i++){
                        shops[data[i].shopid]=data[i].cartGoods;
                    }
                    $scope.all_goods_list = ShopcartService.getAllShopCarInfo(shops);
                });
            }else{
                $scope.all_goods_list = ShopcartService.getAllShopCarInfo();
            }
            //loadCart();
            var orders = [];
            MallChange(3);
            $scope.mall_enturl = projectBaseUrl;//回跳地址
            $scope.goodsContent = {
                totalprice: 0,
                count: 0,
                integration: 0
            };
            //添加商品
            $scope.addgoods = function (shopid, cid, data) {
                ShopcartService.addgoods(shopid, cid, data.goodsid);
                var check = document.getElementById('cg' + shopid + data.goodsid);
                var order;
                if(check.checked == true){
                    for(var i=0;i<orders.length;i++){
                        if(shopid == orders[i].shopid){
                            order = orders[i];
                            break;
                        }
                    }
                    for(var j=0;j<order.list.length;j++){
                        if(order.list[j].goodsid == data.goodsid){
                            order.list[j].num==data.num;
                            break;
                        }
                    }
                    totalPrice();
                }
            }

            //移出商品
            $scope.removegoods = function (shopid, cid, data) {
                var check = document.getElementById('cg' + shopid + data.goodsid);
                var order;
                ShopcartService.removeGoods(shopid, cid, data.goodsid);
                if(check.checked == true){
                    for(var i=0;i<orders.length;i++){
                        if(shopid == orders[i].shopid){
                            order = orders[i];
                            break;
                        }
                    }
                    for(var j=0;j<order.list.length;j++){
                        if(order.list[j].goodsid == data.goodsid){
                            order.list[j].num==data.num;
                            break;
                        }
                    }
                    totalPrice();
                }
            }

            //完全移出商品
            $scope.myque_remove = function (shopid, cid, data) {
                $(".contain-item").fadeIn();
                $scope.clean = function () {
                    ShopcartService.cleanById(shopid, cid, data.goodsid);
                    var check = document.getElementById('cg' + shopid + data.goodsid);
                    var boxes = document.getElementsByName("goods");
                    var order;
                    if(boxes.length == 1){
                        $scope.all_goods_list.length = 0;
                    }
                    if(check.checked == true){
                        for(var i=0;i<orders.length;i++){
                            if(shopid == orders[i].shopid){
                                order = orders[i];
                                break;
                            }
                        }
                        for(var j=0;j<order.list.length;j++){
                            if(order.list[j].goodsid == data.goodsid){
                                order.list.splice(j,1);
                                break;
                            }
                        }
                        for(var k=0;k<orders.length;k++){
                            if(orders[k].list.length == 0){
                                orders.splice(k,1);
                            }
                        }
                        totalPrice();
                    }
                    $(".contain-item").fadeOut();
                }
            }

            /*$scope.buy_change = function (shopid, cid, data, num) {
                ShopcartService.importGoods(shopid, cid, data.goodsid, num);
            }*/

            $scope.input = function (shopid, cid, data, num) {
                for (var i = 0; i < $scope.all_goods_list.length; i++) {
                    var goodsitem = $scope.all_goods_list[i];
                    if (goodsitem.shopid == shopid)
                        break;
                }
                for (var k = 0; k < goodsitem.list.length; k++) {
                    if (goodsitem.list[k].goodsid == data.goodsid) {
                        if (num == undefined ||num == null) {
                            num = 1;
                            input_alert('输入有误')
                        }
                        else if ( num < 1) {
                            num = 1;
                            input_alert('超出购买范围')
                        }
                        else if (num>999) {
                            num = 999;
                            input_alert('超出购买范围')
                        }
                        ShopcartService.importGoods(shopid, cid, data.goodsid, num);
                    }
                }
                var check = document.getElementById('cg' + shopid + data.goodsid);
                var order;
                if(check.checked == true){
                    for(var i=0;i<orders.length;i++){
                        if(shopid == orders[i].shopid){
                            order = orders[i];
                            break;
                        }
                    }
                    for(var j=0;j<order.list.length;j++){
                        if(order.list[j].goodsid == data.goodsid){
                            order.list[j].num==data.num;
                            break;
                        }
                    }
                    totalPrice();
                }
            }
            //添加单个结算订单
            $scope.chooseOne = function (shop_goods, data) {
                var check = document.getElementById('cg' + shop_goods.shopid + data.goodsid);
                var shopBox = document.getElementById('cg' + shop_goods.shopid);
                var shopsCheckedNum = 0;
                var shops = document.getElementsByName("shops");
                var checkAll = document.getElementById('checkAll');
                var shopOrder = {
                    shopid: shop_goods.shopid,
                    shop: shop_goods.shop,
                    list: [],
                    goodscontent: {}
                }
                var allGoodsContent = {};
                var hasShop = false;
                if (orders.length == 0) {
                    orders.push(shopOrder);
                    if (check.checked == true) {
                        for (var i = 0; i < $scope.all_goods_list.length; i++) {
                            var goodsitem = $scope.all_goods_list[i];
                            if (goodsitem.shopid == shop_goods.shopid)
                                break;
                        }
                        for (var k = 0; k < goodsitem.list.length; k++) {
                            if (goodsitem.list[k].goodsid == data.goodsid) {
                                orders[0].list.push(goodsitem.list[k]);
                                if (orders[0].list.length == goodsitem.list.length) {
                                    shopBox.checked = true;
                                }
                                break;
                            }
                        }
                    }
                } else {
                    if (check.checked == true) {
                        for (var i = 0; i < $scope.all_goods_list.length; i++) {
                            var goodsitem = $scope.all_goods_list[i];
                            if (goodsitem.shopid == shop_goods.shopid)
                                break;
                        }
                        for (var k = 0; k < goodsitem.list.length; k++) {
                            if (goodsitem.list[k].goodsid == data.goodsid) {
                                var good = goodsitem.list[k];
                                break;
                            }
                        }
                        for (var m = 0; m < orders.length; m++) {
                            if (orders[m].shopid == shop_goods.shopid) {
                                hasShop = true;
                                var shop = orders[m]
                                break;
                            }
                        }
                        if (hasShop == true) {
                            shop.list.push(good);
                            if (shop.list.length == goodsitem.list.length) {
                                shopBox.checked = true;
                            }
                        } else {
                            orders.push(shopOrder);
                            for (var n = 0; n < orders.length; n++) {
                                if (orders[n].shopid == shop_goods.shopid) {
                                    orders[n].list.push(good);
                                    if (orders[n].list.length == goodsitem.list.length) {
                                        shopBox.checked = true;
                                    }
                                    break;
                                }
                            }
                        }
                    } else {
                        for (var i = 0; i < orders.length; i++) {
                            if (orders[i].shopid == shop_goods.shopid) {
                                for (var j = 0; j < orders[i].list.length; j++) {
                                    if (orders[i].list[j].goodsid == data.goodsid) {
                                        orders[i].list.splice(j, 1);
                                        shopBox.checked = false;
                                        break;
                                    }
                                }
                                if (orders[i].list.length == 0) {
                                    orders.splice(i, 1);
                                    break;
                                }
                            }
                        }
                    }
                }
                for (var a = 0; a < shops.length; a++) {
                    if (shops[a].checked == true) {
                        shopsCheckedNum++;
                    }
                    if (shopsCheckedNum == $scope.all_goods_list.length) {
                        checkAll.checked = true;
                    } else {
                        checkAll.checked = false;
                    }
                }
                totalPrice();
            }
            //添加一个店铺结算订单
            $scope.chooseShop = function (shop_goods) {
                var check = document.getElementById('cg' + shop_goods.shopid);
                var shops = document.getElementsByName("shops");
                var checkAll = document.getElementById('checkAll');
                var shopsCheckedNum = 0;
                var hasShop = false;
                var shopOrder = {
                    shopid: shop_goods.shopid,
                    shop: shop_goods.shop,
                    list: [],
                    goodscontent: {}
                }
                if (check.checked == true) {
                    for (var i = 0; i < shop_goods.list.length; i++) {
                        var goodBox = document.getElementById('cg' + shop_goods.shopid + shop_goods.list[i].goodsid);
                        goodBox.checked = true;
                    }
                    for (var j = 0; j < orders.length; j++) {
                        if (orders[j].shopid == shop_goods.shopid) {
                            hasShop = true;
                            orders[j].list = [];
                            for (var k = 0; k < shop_goods.list.length; k++) {
                                orders[j].list.push(shop_goods.list[k]);
                            }
                            break;
                        }
                    }
                    if (hasShop == false) {
                        for (var k = 0; k < shop_goods.list.length; k++) {
                            shopOrder.list.push(shop_goods.list[k]);
                        }
                        orders.push(shopOrder);
                    }
                } else {
                    for (var i = 0; i < shop_goods.list.length; i++) {
                        var goodBox = document.getElementById('cg' + shop_goods.shopid + shop_goods.list[i].goodsid);
                        goodBox.checked = false;
                    }
                    for (var j = 0; j < orders.length; j++) {
                        if (orders[j].shopid == shop_goods.shopid) {
                            orders.splice(j, 1);
                        }
                    }
                }
                for (var a = 0; a < shops.length; a++) {
                    if (shops[a].checked == true) {
                        shopsCheckedNum++;
                    }
                }
                if (shopsCheckedNum == $scope.all_goods_list.length) {
                    checkAll.checked = true;
                } else {
                    checkAll.checked = false;
                }
                totalPrice();
            }
            //添加所有结算订单
            $scope.chooseAll = function () {
                $scope.goodsContent = {
                    totalprice: 0,
                    count: 0,
                    integration: 0
                };
                var check = document.getElementById('checkAll');
                var boxes = document.getElementsByName("goods");
                var shops = document.getElementsByName("shops");
                if (check.checked == true) {
                    for (var i = 0; i < boxes.length; i++) {
                        boxes[i].checked = true;
                    }
                    for (var j = 0; j < shops.length; j++) {
                        shops[j].checked = true;
                    }
                    orders = [];
                    for (var k = 0; k < $scope.all_goods_list.length; k++) {
                        $scope.chooseShop($scope.all_goods_list[k])
                    }
                } else {
                    orders = [];
                    for (var i = 0; i < boxes.length; i++) {
                        boxes[i].checked = false;
                    }
                    for (var j = 0; j < shops.length; j++) {
                        shops[j].checked = false;
                    }
                }
            }
            //计算单个店铺的总价和订单总价
            function totalPrice(){
                $scope.goodsContent = {
                    totalprice: 0,
                    count: 0,
                    integration: 0
                };
                for (var b = 0; b < orders.length; b++) {
                    var goodsContent = {
                        totalprice: 0,
                        count: 0,
                        integration: 0
                    };
                    for (var c = 0; c < orders[b].list.length; c++) {
                        var goods = orders[b].list[c];
                        goodsContent.totalprice += goods.price * goods.num;
                        goodsContent.count += 1 * goods.num;
                        goodsContent.integration += (goods.price * goods.num) / 100;
                    }
                    orders[b].goodscontent = goodsContent;
                    $scope.goodsContent.totalprice += goodsContent.totalprice;
                    $scope.goodsContent.count += goodsContent.count;
                    $scope.goodsContent.integration += goodsContent.integration;
                }
            }
            //结算
            $scope.account = function () {
                if(orders.length != 0){
                    setCookie('placeorder','order',30);
                    $state.go("placeorder", {order: orders});
                }
            }
            //编辑按钮操作
            $scope.edit = function(shopid){
                var editId = "edit"+shopid;
                var goodsEditId = "goods_edit"+shopid;
                var goodsCompleteId = "edit_complete"+shopid;
                var deleteId = "delete"+shopid;
                cartGoodsEdit(editId,goodsEditId,goodsCompleteId,deleteId);
            }
        }
    ]);
